package 每日一题;

import 我的JDK基础数据结构.HashMap.HashMap;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @description:
 * @author: 小白白
 * @create: 2021-11-24
 **/

public class No423从英文中重建数字 {

    /**
     * 给你一个字符串 s ，其中包含字母顺序打乱的用英文单词表示的若干数字（0-9）。按 升序 返回原始的数字。
     * <p>
     * 示例 1：
     * 输入：s = "owoztneoer"
     * 输出："012"
     * 示例 2：
     * 输入：s = "fviefuro"
     * 输出："45"
     *  
     * 提示：
     * 1 <= s.length <= 105
     * s[i] 为 ["e","g","f","i","h","o","n","s","r","u","t","w","v","x","z"] 这些字符之一
     * s 保证是一个符合题目要求的字符串
     */

    private static Map<Integer, char[]> normalMap = new HashMap<>();
    private static char[][] arr = new char[10][];

    private static LinkedHashMap<Character, Integer> linkedHashMapSort = new LinkedHashMap<>();

    static {
        linkedHashMapSort.put('z', 0);
        linkedHashMapSort.put('w', 2);
        linkedHashMapSort.put('u', 4);
        linkedHashMapSort.put('x', 6);
        linkedHashMapSort.put('g', 8);
        linkedHashMapSort.put('h', 3);
        linkedHashMapSort.put('f', 5);
        linkedHashMapSort.put('s', 7);
        linkedHashMapSort.put('o', 1);
        linkedHashMapSort.put('i', 9);
    }

    static {
        arr[0] = new char[]{'z', 'e', 'r', 'o'};
        arr[1] = new char[]{'o', 'n', 'e'};
        arr[2] = new char[]{'t', 'w', 'o'};
        arr[3] = new char[]{'t', 'h', 'r', 'e', 'e'};
        arr[4] = new char[]{'f', 'o', 'u', 'r'};
        arr[5] = new char[]{'f', 'i', 'v', 'e'};
        arr[6] = new char[]{'s', 'i', 'x'};
        arr[7] = new char[]{'s', 'e', 'v', 'e', 'n'};
        arr[8] = new char[]{'e', 'i', 'g', 'h', 't'};
        arr[9] = new char[]{'n', 'i', 'n', 'e'};
        for (int i = 0; i < arr.length; i++) {
            normalMap.put(i, arr[i]);
        }
    }

    public String originalDigits(String s) {

        Map<Character, Integer> map = new HashMap<>(15);

        char[] arr = s.toCharArray();

        for (char c : arr) {
            map.put(c, map.getOrDefault(c, 0) + 1);
        }

        StringBuilder result = new StringBuilder();
        // zero one two three four five six seven eig nine
        for (Map.Entry<Character, Integer> entry : linkedHashMapSort.entrySet()) {
            int minCount = this.minCount(map, normalMap.get(entry.getValue()), entry.getKey());
            for (int i = 0; i < minCount; i++) {
                result.append(entry.getValue());
            }
        }

        String r = result.toString();
        char[] chars = r.toCharArray();
        Arrays.sort(chars);
        String ss = new String(chars);

        return ss;
    }

    // 可以获得多少个当前数字
    private int minCount(Map<Character, Integer> allMap, char[] arr, char minC) {

        Map<Character, Integer> mapC = new HashMap<>(arr.length);
        for (char c : arr) {
            mapC.put(c, mapC.getOrDefault(c, 0) + 1);
        }

        Integer minCount = allMap.get(minC);

        if (minCount == null || minCount == 0) {
            return 0;
        }

        for (Map.Entry<Character, Integer> entry : mapC.entrySet()) {
            allMap.put(entry.getKey(), allMap.get(entry.getKey()) - (entry.getValue() * minCount));
        }

        return minCount;
    }

    public static void main(String[] args) {
        No423从英文中重建数字 n = new No423从英文中重建数字();
        String result = n.originalDigits("zeroonetwothreefourfivesixseveneightnine");
        System.out.println(result);
    }

}
